'''
基础层。封装定位
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from time import sleep
import logging

logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(name)s %(message)s')


class BasePage():
    base_url = r'http://172.16.22.111:8080'

    def __init__(self, driver):
        self.driver = driver

    def _open(self, url):
        '''实现：打开url页面'''
        full_url = self.base_url + url
        self.driver.get(full_url)
        self.driver.maximize_window()

    def open(self):
        '''实现：打开 不同url地址'''
        self._open(self.url)

    def find_ele(self, loc):
        # 实现元素定位
        ele = self.driver.find_element(*loc)
        return ele
        logging.info(f'定位{loc}成功')

    def input_ele(self, loc, value):
        # 实现：输入框输入数据
        ele = self.find_ele(loc)
        ele.send_keys(value)
        logging.info(f'在{loc}输入{value}成功')

    # 实现按钮点击
    def ele_click(self, loc):
        ele = self.find_ele(loc)
        ele.click()
        logging.info(f'点击{loc}成功')

    # 清空输入框内容
    def ele_clear(self, loc):
        ele = self.find_ele(loc)
        ele.clear()
        logging.info(f'在{loc}清除文本成功')

    # 输入框填写内容
    def ele_select(self, loc, value):
        ele = self.find_ele(loc)
        Select(ele).select_by_visible_text(value)
        logging.info(f'定位{loc}成功，选择{value}成功')

    # 抓取弹出框内容
    def get_alert_text(self):
        text = self.driver.switch_to.alert.text
        return text

    # 处理弹窗内容
    def alert_click(self):
        self.driver.switch_to.alert.accept()
